<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="110px" label="项目名称" prop="itemName">
        <el-select v-model="queryParams.itemName" placeholder="请选择项目名称" clearable>
          <el-option
            v-for="dict in dict.type.sport_item"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label-width="110px" label="项目类型" prop="itemType">
        <el-select v-model="queryParams.itemType" placeholder="请选择个人赛或团体赛" clearable>
          <el-option
            v-for="dict in dict.type.item_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label-width="110px" label="项目比赛地点" prop="itemAddress">
        <el-select v-model="queryParams.itemAddress" placeholder="请选择项目比赛地点" clearable>
          <el-option
            v-for="dict in dict.type.competition_set"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="项目比赛性别要求" prop="itemSex">-->
      <!--        <el-select v-model="queryParams.itemSex" placeholder="请选择项目比赛性别要求" clearable>-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.sys_user_sex"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label-width="110px" label="项目比赛时间" prop="itemDate">
        <el-date-picker clearable
                        v-model="queryParams.itemDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择项目比赛时间">
        </el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="报名开始时间" prop="itemStardate">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.itemStardate"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择报名开始时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="报名结束时间" prop="itemEnddate">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.itemEnddate"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择报名结束时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="项目负责人id" prop="itemJudgementid">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.itemJudgementid"-->
      <!--          placeholder="请输入项目负责人id"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label-width="120px" label="项目负责人姓名" prop="itemJudgementname">
        <el-input
          v-model="queryParams.itemJudgementname"
          placeholder="请输入项目负责人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:item:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:item:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:item:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:item:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="比赛项目id" align="center" prop="id" />-->
      <el-table-column label="项目名称" align="center" prop="itemName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sport_item" :value="scope.row.itemName"/>
        </template>
      </el-table-column>
      <el-table-column label="项目分类" align="center" prop="itemClassify">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.item_classify" :value="scope.row.itemClassify"/>
        </template>
      </el-table-column>
      <el-table-column label="项目类型" align="center" prop="itemType">
        <template slot-scope="scope">
          <dict-tag ty :options="dict.type.item_type" :value="scope.row.itemType"/>
        </template>
      </el-table-column>
      <el-table-column label="报名人数要求" align="center" prop="itemPeoNum"/>
      <el-table-column label="项目比赛地点" align="center" prop="itemAddress">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.competition_set" :value="scope.row.itemAddress"/>
        </template>
      </el-table-column>
      <el-table-column label="项目比赛性别要求" align="center" prop="itemSex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.itemSex"/>
        </template>
      </el-table-column>
      <el-table-column label="项目比赛时间" align="center" prop="itemDate" width="180">
      </el-table-column>
      <el-table-column label="报名开始时间" align="center" prop="itemStardate" width="180">
      </el-table-column>
      <el-table-column label="报名结束时间" align="center" prop="itemEnddate" width="180">
      </el-table-column>
      <el-table-column label="项目负责人id" align="center" prop="itemJudgementid"/>
      <el-table-column label="项目负责人姓名" align="center" prop="itemJudgementname"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:item:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:item:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改比赛项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <div style="font-size: 18px;color: #C03639">注意：为方便计算，团体赛中，计时的项目选择径赛，否则选择田赛</div>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label-width="110px" label="项目名称" prop="itemName">
              <el-select clearable style="width: 240px" v-model="form.itemName" placeholder="请选择项目名称">
                <el-option
                  v-for="dict in dict.type.sport_item"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="110px" label="项目分类" prop="itemClassify">
              <el-select clearable style="width: 240px" v-model="form.itemClassify" placeholder="请选择项目分类">
                <el-option
                  v-for="dict in dict.type.item_classify"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label-width="110px" label="项目类型" prop="itemType">
              <el-select clearable style="width: 240px" v-model="form.itemType" placeholder="请选择个人赛或团体赛">
                <el-option
                  v-for="dict in dict.type.item_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="110px" label="报名人数" prop="itemPeoNum">
              <el-input style="width: 240px" v-model="form.itemPeoNum" placeholder="请输入报名人数要求"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label-width="110px" label="项目比赛地点" prop="itemAddress">
              <el-select clearable style="width: 240px" v-model="form.itemAddress" placeholder="请选择项目比赛地点">
                <el-option
                  v-for="dict in dict.type.competition_set"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="110px" label="性别要求" prop="itemSex">
              <el-select clearable style="width: 240px" v-model="form.itemSex" placeholder="请选择项目比赛性别要求">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label-width="110px" label="比赛时间" prop="itemDate">
              <el-date-picker clearable
                              v-model="form.itemDate"
                              type="datetime"
                              style="width: 240px"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择项目比赛时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="110px" label="报名开始时间" prop="itemStardate">
              <el-date-picker clearable
                              v-model="form.itemStardate"
                              type="datetime"
                              style="width: 240px"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择报名开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label-width="110px" label="报名结束时间" prop="itemEnddate">
              <el-date-picker clearable
                              v-model="form.itemEnddate"
                              type="datetime"
                              style="width: 240px"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择报名结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="110px" label="项目负责人" prop="itemJudgementid">
              <el-select clearable style="width: 240px" v-model="form.itemJudgementid" placeholder="请选择项目负责人">
                <el-option
                  v-for="Judgement in judgmentList"
                  :key="Judgement.userName"
                  :label="Judgement.nickName"
                  :value="Judgement.userName">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listItem, getItem, delItem, addItem, updateItem} from "@/api/system/item";
import {listJudgment} from "@/api/system/user";

export default {
  name: "Item",
  dicts: ['sys_user_sex', 'sport_item', 'competition_set', 'item_type', 'item_classify'],
  data() {
    return {
      // 裁判列表
      judgmentList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 比赛项目表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
        itemType: null,
        itemPeoNum: null,
        itemAddress: null,
        itemSex: null,
        itemDate: null,
        itemStardate: null,
        itemEnddate: null,
        itemJudgementid: null,
        itemJudgementname: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemName: [
          { required: true, message: '请选择比赛项目', trigger: 'change' }
        ],
        itemClassify: [
          { required: true, message: '请选择项目分类', trigger: 'change' }
        ],
        itemType: [
          { required: true, message: '请选择项目类型', trigger: 'change' }
        ],
        itemAddress: [
          { required: true, message: '请选择项目比赛地点', trigger: 'change' }
        ],
        itemSex: [
          { required: true, message: '请选择性别要求', trigger: 'change' }
        ],
        itemDate: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ],
        itemStardate: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ],
        itemEnddate: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ],
        itemJudgementid: [
          { required: true, message: '请选择裁判', trigger: 'change' }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getJudgmentList();
  },
  methods: {
    /** 获取裁判列表 */
    getJudgmentList() {
      this.loading = true;
      listJudgment().then(response => {
        this.judgmentList = response.rows;
        console.log(this.judgmentList)
        this.loading = false;
      })
    },
    /** 查询比赛项目列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        itemName: null,
        itemAddress: null,
        itemSex: null,
        itemDate: null,
        itemStardate: null,
        itemEnddate: null,
        itemJudgementid: null,
        itemJudgementname: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加比赛项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getItem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改比赛项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除比赛项目编号为"' + ids + '"的数据项？').then(function () {
        return delItem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/item/export', {
        ...this.queryParams
      }, `item_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
